package com.py.credit.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.dao.ClassifyDao;
import com.py.credit.entity.Classify;
import com.py.credit.entity.SysUser;
import com.py.credit.entity.TreeIndexc;
import com.py.credit.service.ClassifyService;
import com.py.framework.core.common.Page;

@Service
public class ClassifyServiceImpl implements ClassifyService {

	@Autowired
	private ClassifyDao classifyDao;

	@Override
	public List<Classify> queryCategoryForPage(String name, Page page, String mainType) {
		return classifyDao.queryCategoryForPage(name, page, mainType);
	}

	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void addIndexAndCategory(List<Map<String, Object>> treelist, RestServiceResult<SysUser> user, String topId) {
		List<Map<String, Object>> indexList = new ArrayList<>();
		List<Map<String, Object>> categoryList = new ArrayList<>();
		List<Map<String, Object>> indexList2 = new ArrayList<>();
		List<Map<String, Object>> categoryList1 = new ArrayList<>();
		List<Map<String, Object>> indexList3 = new ArrayList<>();
		List<Map<String, Object>> categoryList2 = new ArrayList<>();
		List<Map<String, Object>> indexList4 = new ArrayList<>();
		List<Map<String, Object>> categoryList3 = new ArrayList<>();
		String userName = user.getData().getRealName();
		classifyDao.deleteIndexCategoryByTopId(topId);
		classifyDao.deleteCategoryByTopId(topId);

		for (Map map : treelist) {
			map.put("username", userName);
			if (("index").equals(map.get("type"))) {
				indexList.add(map);
			}
			if (map.get("type").equals("category")) {
				categoryList.add(map);
			}
		}
		//
		for (Map category : categoryList) {
			if (category.get("level").equals(1)) {
				categoryList1.add(category);
			}
			if (category.get("level").equals(2)) {
				categoryList2.add(category);
			}
			if (category.get("level").equals(3)) {
				categoryList3.add(category);
			}
		}

		for (Map index : indexList) {
			if (index.get("level").equals(2)) {
				indexList2.add(index);
			}
			if (index.get("level").equals(3)) {
				indexList3.add(index);
			}
			if (index.get("level").equals(4)) {
				indexList4.add(index);
			}
		}

		for (Map category : categoryList1) {
			String id = (String) category.get("id");
			String uuid = UUID.randomUUID().toString().replaceAll("-", "") + System.currentTimeMillis();
			for (Map category2 : categoryList2) {
				String category2id = (String) category2.get("id");
				String category2uuid = UUID.randomUUID().toString().replaceAll("-", "") + System.currentTimeMillis();
				if (category2.get("pId").equals(id)) {
					category2.put("pId", uuid);
				}
				for (Map category3 : categoryList3) {
					String category3id = (String) category3.get("id");
					String category3uuid = UUID.randomUUID().toString().replaceAll("-", "")
							+ System.currentTimeMillis();
					if (category3.get("pId").equals(category2id)) {
						category3.put("pId", category2uuid);
					}
					for (Map index4 : indexList4) {
						if (index4.get("pId").equals(category3id)) {
							index4.put("pId", category3uuid);
						}
					}
					category3.put("id", category3uuid);
				}
				for (Map index3 : indexList3) {
					if (index3.get("pId").equals(category2id)) {
						index3.put("pId", category2uuid);
					}
				}
				category2.put("id", category2uuid);
			}
			for (Map index2 : indexList2) {
				if (index2.get("pId").equals(id)) {
					index2.put("pId", uuid);
				}
			}
			category.put("id", uuid);
			category.put("pId", topId);
		}

		if (!CollectionUtils.isEmpty(indexList)) {
			classifyDao.addIndexCategory(indexList);
		}

		if (!CollectionUtils.isEmpty(categoryList)) {
			classifyDao.addCategory(categoryList);
		}
	}

	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void addTopCategory(Classify classify) {
		String top_parent_id = UUID.randomUUID().toString().replaceAll("-", "");
		classify.setId(top_parent_id);
		classify.setTop_parent_id(top_parent_id);
		classifyDao.addTopCategory(classify);

		// 添加该分类所引用的分类树
		if (StringUtils.isNotEmpty(classify.getRelated_Cat())) {
			List<TreeIndexc> treeList = classifyDao.getTreeByTopId(classify.getRelated_Cat());
			List<TreeIndexc> indexList = new ArrayList<>();
			List<TreeIndexc> categoryList = new ArrayList<>();
			List<TreeIndexc> indexList2 = new ArrayList<>();
			List<TreeIndexc> categoryList1 = new ArrayList<>();
			List<TreeIndexc> indexList3 = new ArrayList<>();
			List<TreeIndexc> categoryList2 = new ArrayList<>();
			List<TreeIndexc> indexList4 = new ArrayList<>();
			List<TreeIndexc> categoryList3 = new ArrayList<>();
			for (TreeIndexc tree : treeList) {
				tree.setCjr(classify.getCjr());
				tree.setMainType(classify.getMainType());
				tree.setTopPid(classify.getId());
				if (("index").equals(tree.getType())) {
					indexList.add(tree);
				}
				if (tree.getType().equals("category")) {
					categoryList.add(tree);
				}
			}
			for (TreeIndexc category : categoryList) {
				if (category.getLevel() == 1) {
					categoryList1.add(category);
				}
				if (category.getLevel() == 2) {
					categoryList2.add(category);
				}
				if (category.getLevel() == 3) {
					categoryList3.add(category);
				}
			}

			for (TreeIndexc index : indexList) {
				if (index.getLevel() == 2) {
					indexList2.add(index);
				}
				if (index.getLevel() == 3) {
					indexList3.add(index);
				}
				if (index.getLevel() == 4) {
					indexList4.add(index);
				}
			}
			for (TreeIndexc category : categoryList1) {
				String id = category.getId();
				String uuid = UUID.randomUUID().toString().replaceAll("-", "") + System.currentTimeMillis();
				for (TreeIndexc category2 : categoryList2) {
					String category2id = category2.getId();
					String category2uuid = UUID.randomUUID().toString().replaceAll("-", "")
							+ System.currentTimeMillis();
					if (category2.getpId().equals(id)) {
						category2.setpId(uuid);
					}
					for (TreeIndexc category3 : categoryList3) {
						String category3id = category3.getId();
						String category3uuid = UUID.randomUUID().toString().replaceAll("-", "")
								+ System.currentTimeMillis();
						if (category3.getpId().equals(category2id)) {
							category3.setpId(category2uuid);
						}
						for (TreeIndexc index4 : indexList4) {
							if (index4.getpId().equals(category3id)) {
								index4.setpId(category3uuid);
							}
						}
						category3.setId(category3uuid);
					}
					for (TreeIndexc index3 : indexList3) {
						if (index3.getpId().equals(category2id)) {
							index3.setpId(category2uuid);
						}
					}
					category2.setId(category2uuid);
				}
				for (TreeIndexc index2 : indexList2) {
					if (index2.getpId().equals(id)) {
						index2.setpId(uuid);
					}
				}
				category.setId(uuid);
				category.setpId(classify.getId());
			}

			if (!CollectionUtils.isEmpty(indexList)) {
				classifyDao.addIndexCategoryByEntity(indexList);
			}

			if (!CollectionUtils.isEmpty(categoryList)) {
				classifyDao.addCategoryByEntity(categoryList);
			}
		}
	}

	@Override
	public List<Classify> getTopCategory(String mainType) {
		return classifyDao.getTopCategory(mainType);
	}

	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void deleteCategoryById(List<String>  id) {
		classifyDao.deleteCategoryById(id);
		classifyDao.deleteXypjIndexCategoryByTopId(id);
	}

	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void updateCategory(Classify classify) {
		classifyDao.updateCategory(classify);

	}

	@Override
	public List<TreeIndexc> getTreeByTopId(String topId) {
		List<TreeIndexc> treeList = classifyDao.getTreeByTopId(topId);
		return treeList;
	}

	@Override
	public Classify getCategoryById(String id) {
		return classifyDao.getCategoryById(id);
	}

	@Override
	public int findInCaModel(String id) {
		return classifyDao.findInCaModel(id);
	}

	@Override
	public Integer getCateModelByid(String id) {
		String[] ids = id.split("_");
		String id1 = "";
		if (ids.length > 0) {
			id1 = ids[0];
		}
		return classifyDao.selectCateModelByid(id1);
	}

	@Override
	public Integer selectCateModelByIdDel(String id) {
		return classifyDao.selectCateModelByIdDel(id);
	}

}
